package org.example.lanchain.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.example.lanchain.bean.ApiKey;

import java.time.LocalDateTime;

/**
 * API Key数据访问层
 */
@Mapper
public interface ApiKeyMapper extends BaseMapper<ApiKey> {

    /**
     * 更新API Key使用统计
     */
    @Update("UPDATE api_keys SET usage_count = usage_count + 1, last_used_at = #{lastUsedAt} WHERE api_key = #{apiKey}")
    int updateUsageStats(@Param("apiKey") String apiKey, @Param("lastUsedAt") LocalDateTime lastUsedAt);

    /**
     * 根据API Key查找记录
     */
    ApiKey selectByApiKey(@Param("apiKey") String apiKey);
}